const PIETWEEN = window.PIETWEEN;
/*
用于定位飞行
 */
export const GlobeFlyTo = function flyTo(
  globe: any,
  targetLon: number,
  targetLat: number,
  targetHeight: number,
  time: number
) {
  PIETWEEN.flyTo({
    globe,
    destination: { lon: targetLon, lat: targetLat, height: targetHeight },
    orientation: { heading: 0, pitch: 0, roll: 0 },
    time: time,
  });
};

// 地图工具动态变化：定位、放大、缩小
export const AnimatTo = (
  globe: any,
  scaleDistance: number,
  lng: number,
  lat: number,
  pitching: number,
  heading: number
) => {
  let zoomDownToTween = PIETWEEN.TweenCamera.zoomToTween(
    globe,
    scaleDistance,
    PIETWEEN.Easing.Quintic.Out,
    3000
  );
  let moveToTween = PIETWEEN.TweenCamera.moveToTween(
    globe,
    lng,
    lat,
    PIETWEEN.Easing.Quintic.Out,
    3000
  );
  let pitchToTween = PIETWEEN.TweenCamera.pitchToTween(
    globe,
    pitching,
    PIETWEEN.Easing.Quintic.Out,
    3000
  );
  let headingToTween = PIETWEEN.TweenCamera.headingToTween(
    globe,
    heading,
    PIETWEEN.Easing.Quintic.Out,
    3000
  );
  let tween = new PIETWEEN.Tween();
  tween.chain(moveToTween, zoomDownToTween, pitchToTween, headingToTween);
  tween.start();
  globe.refresh(false, false);
};

// 比例尺 -> 分辨率 -> 距离
export const ScaleToDistance = (dScale: any, globe: any, globeControl: any) => {
  const EARTH_RADIUS = 6378137;
  const MMPERINCH = 25.4000508001016;
  const dDPIRatio = 96 / MMPERINCH;
  const EARTH_CIRCUM = 2 * Math.PI * EARTH_RADIUS;

  //scale转换为Resolution
  let dResolution = (dScale * dDPIRatio * 10000 * EARTH_CIRCUM) / 10;
  //Resolution转换为distance
  // return (2 * 256 * EARTH_CIRCUM) / Math.abs(dResolution);
  let camera = globe.getWorldCamera();
  var dPixelPerMeter = dResolution / EARTH_CIRCUM;
  let fov = camera.getFov();
  let distance =
    (globeControl.getCanvas().height * 0.5) /
    (dPixelPerMeter * Math.tan(fov / 2));
  return distance;
};

export default {
  GlobeFlyTo,
  AnimatTo,
  ScaleToDistance,
};
